package com.shiliao.app.activity.interlocution.ui;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;

import com.shiliao.app.R;
import com.shiliao.app.activity.interlocution.presenter.MyQuestionPresenter;
import com.shiliao.app.activity.interlocution.view.MyQuestionView;
import com.shiliao.app.manager.YMUserManager;
import com.shiliao.app.utils.MyWebView;
import com.shiliao.app.utils.WebSetUtil;
import com.shiliao.app.utils.YMServerUtil;
import com.shiliao.app.weight.ProgressWebView;
import com.yimei.framework.base.ui.BaseFragment;
import com.yimei.framework.util.LogUtil;

import butterknife.BindView;

/**
 * Created by yinzhiqiang on 2017/9/11.
 */

public class MyQuestionFragment extends BaseFragment implements MyQuestionView {
    @BindView(R.id.webview)
    ProgressWebView webview;

    @BindView(R.id.swip_refresh)
    SwipeRefreshLayout refresh;
    //private MyWebView mWebView;

    private MyQuestionPresenter myQuestionPresenter;
    private String userId;
    private MyWebView mWebView;

    @Override
    public int getLayoutId() {
        return R.layout.activity_daily_knowledge;
    }

    @Override
    protected void initFragmentView() {
        super.initFragmentView();
        WebSetUtil.setWebView(webview);
        if (myQuestionPresenter == null) {
            myQuestionPresenter = new MyQuestionPresenter(this);
        }
        userId = YMUserManager.getUserId(getContext());
        myQuestionPresenter.requestMyQuestionList(userId);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                myQuestionPresenter.requestMyQuestionList(userId);
            }
        });
        initData();
    }

    private void initData() {
        Intent intent = new Intent(getContext(), CommonProblemDetailActivity.class);
        mWebView = new MyWebView(webview, getContext(), YMServerUtil.LATEST_PROBLEM_COTURL, intent,
                refresh, YMServerUtil.LATEST_PROBLEM_REFERASHURL);
        webview.setWebViewClient(mWebView);
    }

    @Override
    public void loadWebView(final String content) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (refresh.isRefreshing()) {
                    refresh.setRefreshing(false);
                }
                webview.loadDataWithBaseURL(YMServerUtil.BASE_URL,
                        content, "text/html", "utf-8", null);
                //     webview.loadData(content,"text/html; charset=UTF-8", null);
            }
        });
    }

    @Override
    public void onResume() {

        super.onResume();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        LogUtil.e("yzq", ".....................");
        if (isVisibleToUser) {
            if (myQuestionPresenter != null) {
                myQuestionPresenter.requestMyQuestionList(userId);
            }
        }

    }
}
